package data.mxgraph;

import java.util.Hashtable;
import java.util.Iterator;

import javax.swing.SwingConstants;

import com.mxgraph.layout.hierarchical.mxHierarchicalLayout;
import com.mxgraph.model.mxGraphModel;
import com.mxgraph.util.mxConstants;
import com.mxgraph.view.mxGraph;
import com.mxgraph.view.mxStylesheet;

import data.ActionList;
import data.model.Task;
import data.model.TaskGraph;
import data.model.TaskGraph.Vertex;

public class mxTaskGraph {
		
	private static mxGraphModel model;
	
	private static mxGraph graph;
	
	private static mxHierarchicalLayout lay;
	
	private static mxStylesheet stylesheet;
	
	private static int curX = 500, curY = 0;
	
	public mxTaskGraph()
	{
		model = new mxGraphModel();
		setStyles();
		graph = new mxGraph(model, stylesheet);
		lay = new mxHierarchicalLayout(graph, SwingConstants.WEST);
		lay.setIntraCellSpacing(2 * lay.getIntraCellSpacing());
		//lay.setInterHierarchySpacing(2 * lay.getInterHierarchySpacing());
		
	}
	
	public static void clear()
	{
		model.clear();
		graph = new mxGraph(model, stylesheet);
		lay = new mxHierarchicalLayout(graph, SwingConstants.WEST);
		lay.setIntraCellSpacing(2 * lay.getIntraCellSpacing());
	}
	
	private void setStyles()
	{
		stylesheet = new mxStylesheet();
		//Hashtable<String, Object> vtxStyle = (Hashtable<String, Object>) stylesheet.getDefaultVertexStyle();
//		style.put(mxConstants.STYLE_SHAPE, mxConstants.SHAPE_RECTANGLE);
//		style.put(mxConstants.STYLE_OPACITY, 0);
//		style.put(mxConstants.STYLE_FONTCOLOR, "#774400");
//		stylesheet.putCellStyle("ROUNDED", vtxStyle);
		
		//stylesheet.setDefaultVertexStyle(vtxStyle);
	}
	
	/**
	 * insert a task vertex into the graph
	 */
	public static void addTask(Task t)
	{
		String id = "t" + t.getTaskID();
		String actName = ActionList.getAction(t.getAction()).getActName();
		//System.out.println(t.getTaskName() + id);
		Object parent = graph.getDefaultParent();
		
		if( curY < 300)
		{
			curY += 20;
		}
		else
		{
			curY = 100;
		}
		
		model.beginUpdate();
		graph.insertVertex(parent, id, t.getTaskName() + " - " + actName, curX, curY, 80, 30);
		lay.execute(parent);
		model.endUpdate();
		graph.refresh();
		
	}
	
	public static void removeTask(int taskID)
	{
		
		String id = "t" + taskID;
		Object [] rem = {model.getCell(id)};
		
		model.beginUpdate();
		graph.removeCells(rem, true);
		lay.execute(graph.getDefaultParent());
		model.endUpdate();
		graph.refresh();
		
	}
	
	public static void removeDep(int srcID, int destID)
	{
		String id = srcID + "t" + destID;
		Object [] rem = {model.getCell(id)};
		
		model.beginUpdate();
		graph.removeCells(rem, false);
		lay.execute(graph.getDefaultParent());
		model.endUpdate();
		graph.refresh();
		
	}
	
	public static void addDep(int srcID, int destID)
	{
		Object parent = graph.getDefaultParent();
		model.beginUpdate();
		graph.insertEdge(parent, srcID + "t" + destID, null,
				model.getCell("t" + srcID), model.getCell("t" + destID));
		lay.execute(parent);
		model.endUpdate();
		graph.refresh();
	}

	public static mxGraph getGraph() {
		return graph;
	}
	
	public static void refresh()
	{
		model.beginUpdate();
		lay.execute(graph.getCurrentRoot());
		model.endUpdate();
		graph.refresh();
	}

}
